package fr.enseirb.webxml.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.Properties;


import fr.enseirb.webxml.util.ServletToolkit;

/**
 * Servlet implementation class AboutServlet
 */
public class AboutServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private static String teacherName = "";

	private String[][] students;
	private Properties groupeProps;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public AboutServlet() {
		super();

		groupeProps = new Properties();
		groupeProps.setProperty("studentsNumber", "2");
		groupeProps.setProperty("class", "I2");
		groupeProps.setProperty("group", "Groupe 4");

		students = new String[2][2];
		students[0][0] = "Arvid";
		students[0][1] = "Jakobsson";
		students[1][0] = "Oussama";
		students[1][1] = "Farouq";
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String sResponse = "";

		Properties params = ServletToolkit.parseURLParams(request);
		if (params.containsKey("action")) {
			sResponse = getResponse(params.getProperty("action"), params);
		} else if (request.getRequestURI().contains("about/teacher")) {
			sResponse = ServletToolkit.readFile("/resources/html/common/about_teacher.html");
		} else if (request.getRequestURI().contains("about/xml")) {
			response.setHeader("Content-Type", "application/xml");
			sResponse = getResponseXML();
		} else {
			sResponse = getResponse("full", params);
		}

		ServletToolkit.writeResponse(response, sResponse);
	}


	protected String getResponseXML() {
		String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
		xml += "<about "
			+ "studentsNumber=\"" + students.length + "\" "
			+ "group=\"" + groupeProps.getProperty("group") + "\" "
			+ "class=\"" + groupeProps.getProperty("class") + "\" "
			+ "teacher=\"" + teacherName + "\">";
		for (String[] s : students) {
			xml += "<student firstName=\"" + s[0] + "\" lastName=\"" + s[1] + "\" />\n";
		}
		xml += "</about>\n";
		return xml;
	}

	protected String getResponse(String action, Properties params) {
		String response = "";

		if (groupeProps.containsKey(action)) {
			response = groupeProps.getProperty(action);
		} else if (action.equals("teacher")) {
			response = teacherName;
		} else if (action.equals("full")) {
			String[] infoProps = {
				groupeProps.getProperty("class"),
				groupeProps.getProperty("group")
			};

			LinkedList<String> info =
				new LinkedList<String>(Arrays.asList(infoProps));

			for (String[] s : students) {
				info.add(s[0] + " " + s[1]);
			}

			String result = "";
			for (String p : info) {
				if (result.length() != 0) result += " / ";
				result += p;
			}
			response = result;
		} else if (action.equals("firstName") || action.equals("lastName")) {
			try {
				int index = Integer.parseInt(params.getProperty("studentId")) - 1;
				response = students[index][action.equals("firstName") ? 0 : 1];
			} catch (Exception e) {
				response = "L'etudiant n'existe pas.";
			}
		}

		return response + "\n";
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)ñ
	 * @Override
	 */
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		teacherName = ServletToolkit.getPostData(req);
		ServletToolkit.writeResponse(resp, "OK");
	}
}
